﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using Telerik.Web.UI;
using ud_gSDMS.Code_Service;
using System.Linq;
using System.Web.UI;

namespace ud_gSDMS.testing
{
    public partial class Error_Lists : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        private int id = -1;
        private SiteUser _siteUser;

        void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //if (!UserCanEditPage() || !UserCanViewPage())
                //{
                //    WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
                //}

                _siteUser = SiteUtils.GetCurrentSiteUser();

                if (!Page.IsPostBack)
                {
                    PopulateLabels();
                    PopulateControls();
                }
            }
            catch
            {
            }
        }
        private void PopulateLabels()
        {

        }
        private void PopulateControls()
        {
            

            LoadControl();
            LoadGrid();
        }
        private void LoadGrid()
        {
            var data = gSDMS_BL.ud_gSDMS_PROJECT_TEST_ERROR_Lst(id);
            
            if (ViewState["SortExpression"] != null)
            {
                var dv = data.DefaultView;
                dv.Sort = ViewState["SortExpression"].ToString();
                data = dv.Table;
            }

            
            grdError_Lists. DataSource = data;
            grdError_Lists.DataBind();

            ViewState["listError"] = data;
        }
        private void LoadControl()
        {
            var temp = gSDMS_BL.ud_gSDMS_PROJECT_TEST_Get(id);

            hidProjectID.Value = temp.Project_ID.ToString();

            lbProject_Code.Text = temp.ud_gSDMS_PROJECT.Code;
            lbProject_Name.Text = temp.ud_gSDMS_PROJECT.Name;
            lbStatus.Text = temp.ud_gSDMS_CAT_PROJECT_TEST_STATUS.Name;

            lbTotal_Errors.Text = (from d in temp.ud_gSDMS_PROJECT_TEST_ERRORs
                                    select d).Count().ToString();
            lbOpen.Text = (from d in temp.ud_gSDMS_PROJECT_TEST_ERRORs
                                                     where d.ud_gSDMS_CAT_ERROR_STATUS.Name == "Open"
                                                     select d).Count().ToString();
            lbPending.Text = (from d in temp.ud_gSDMS_PROJECT_TEST_ERRORs
                                             where d.ud_gSDMS_CAT_ERROR_STATUS.Name == "Pending"
                                             select d).Count().ToString();
            lbFixed.Text = (from d in temp.ud_gSDMS_PROJECT_TEST_ERRORs
                                           where d.ud_gSDMS_CAT_ERROR_STATUS.Name == "Closed"
                                           select d).Count().ToString();
            if (gSDMS_BL.mp_User_IsRole(_siteUser.UserId, "3|1"))
            {
                btnCloseProject.Visible = true;
                if (temp.ud_gSDMS_CAT_PROJECT_TEST_STATUS.Name != "Closed")
                {
                    btnAdd_New.Visible = true;
                    grdError_Lists.Columns[8].Visible = true;
                }
                

            }

            if (gSDMS_BL.mp_User_Is_In_Project(_siteUser.UserId , (int)temp.Project_ID))
            {
                if (temp.ud_gSDMS_CAT_PROJECT_TEST_STATUS.Name != "Closed")
                    grdError_Lists.Columns[7].Visible = true;
            }

            if (gSDMS_BL.mp_User_IsRole(_siteUser.UserId, "1"))
            {
                btnCloseProject.Visible = true;
                if (temp.ud_gSDMS_CAT_PROJECT_TEST_STATUS.Name != "Closed")
                {
                    btnAdd_New.Visible = true;
                    grdError_Lists.Columns[8].Visible = true;
                    grdError_Lists.Columns[7].Visible = true;
                }


            }
        }

        #region event
        private void grdError_Lists_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdError_Lists.PageIndex = e.NewPageIndex;
            LoadGrid();
        }
        private void grdError_Lists_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (ViewState["listError"] != null)
            {
                string value ;

                switch (e.CommandName)
                {
                    case "fix" :
                        value = e.CommandArgument.ToString();
                        WebUtils.SetupRedirect(this, SiteRoot + "/gsdmsmodule/testing/Fix_Error.aspx?pageid=9&testid=" + id + "&error=" + value);
                        break;
                    case "close" :
                        value = e.CommandArgument.ToString();
                        WebUtils.SetupRedirect(this, SiteRoot + "/gsdmsmodule/testing/Close_Error.aspx?pageid=9&testid=" + id + "&error=" + value);
                        break;

                }
            }
        }
        void grdError_Lists_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LoadGrid();
        }

        void grdError_Lists_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["Direction"] == null) ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " ASC") ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " DESC") ViewState["Direction"] = " ASC";
            string strSortExp = e.SortExpression + ViewState["Direction"].ToString();
            ViewState["SortExpression"] = strSortExp;
            
            LoadGrid();
        }

        void btnExport_Excel_Click(object sender, EventArgs e)
        {
            var s = ViewState["listError"];
            if (s != null)
            {
                hiddenGrid.DataSource = s;
                hiddenGrid.DataBind();

                var name = ("Error-List" + lbProject_Code.Text);
                hiddenGrid.ExportSettings.FileName = name;
                hiddenGrid.ExportSettings.IgnorePaging = true;
                hiddenGrid.ExportSettings.OpenInNewWindow = true;
                hiddenGrid.MasterTableView.ExportToExcel();

                hiddenGrid.DataSource = null;
            //    ASPxGridViewExporter1.WriteXlsToResponse(name);

            }
        }
        #endregion

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            Load += Page_Load;
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            grdError_Lists.PageIndexChanging += grdError_Lists_PageIndexChanging;
            grdError_Lists.RowCommand += grdError_Lists_RowCommand;
            grdError_Lists.RowDeleting += grdError_Lists_RowDeleting;
            grdError_Lists.Sorting += grdError_Lists_Sorting;
            grdError_Lists.RowDataBound += new GridViewRowEventHandler(grdError_Lists_RowDataBound);

            btnAdd_New.Click += new EventHandler(btnAdd_New_Click);
            btnCloseProject.Click += new EventHandler(btnCloseProject_Click);
            //btnFinish.Click += btnFinish_Click;
            SuppressPageMenu();
        }

        

        void grdError_Lists_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (_siteUser == null || string.IsNullOrEmpty(hidProjectID.Value)) return;
            bool isAdmin = gSDMS_BL.mp_User_IsRole(_siteUser.UserId, "1");
            if (gSDMS_BL.mp_User_Is_In_Project(_siteUser.UserId, Convert.ToInt32(hidProjectID.Value)) == false
                && isAdmin == false)
            {
                return;
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string text = ((DataBoundLiteralControl)e.Row.Cells[4].Controls[0]).Text;
                if (text != "Closed")
                {
                    e.Row.FindControl("btn_Update").Visible = true;
                }

            }
        }

        void btnCloseProject_Click(object sender, EventArgs e)
        {
            WebUtils.SetupRedirect(this, SiteRoot + "/gsdmsmodule/testing/CloseTestProject.aspx?pageid=9&testid=" + id);   
        }

        void btnAdd_New_Click(object sender, EventArgs e)
        {
            WebUtils.SetupRedirect(this, SiteRoot + "/gsdmsmodule/testing/Add_Error.aspx?pageid=9&testid=" + id);           
        }


        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
            id = WebUtils.ParseInt32FromQueryString("testid", id);
        }

        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            //config = new ContactFormConfiguration(settings);
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion
    }
}